package com.mindao.app.ued.controller;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mindao.app.common.spring.controller.BaseController;
import com.mindao.app.common.spring.service.util.JsonResult;
import com.mindao.app.ued.model.Event;
import com.mindao.app.ued.model.EventPrize;
import com.mindao.app.ued.service.EventPrizeService;
import com.mindao.utils.config.EaCommonUtils;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：
/*
/* 创 建 人：李国才
/* 创建时间：2016年6月25日 下午12:02:58 	
 **/@Controller
 @RequestMapping("/ued/eventprize")
public class EventPrizeController extends BaseController<EventPrize> {
		private final Logger log = LoggerFactory.getLogger(EventPrizeController.class);
		@Autowired
		protected EventPrizeService eventPrizeService;
		
		/**O
		 * 保存奖品
		 * @param request
		 * @param response
		 * @param session
		 * @param rhs
		 * @return
		 * @throws Exception
		 */
	    @ResponseBody
		@RequestMapping( method = RequestMethod.POST)
		public JsonResult savePrize(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
			String uuid=getpara("eventUuid");
			log.info("1111111111111111");
			Event event=(Event)EaCommonUtils.getObjectByUuid(uuid, "Event");
			
	    	String[] names=request.getParameterValues("name");
	    	String[] ids=request.getParameterValues("id");
	    	String[] grades=request.getParameterValues("grade");
	    	String[] hitRatios=request.getParameterValues("hitRatio");
	    	String[] moneys=request.getParameterValues("money");
	    	String[] images=request.getParameterValues("image");
	    	log.info("222222222222222222");
	    	EventPrize ep=null;
	    	BigDecimal sumHitRatio= new BigDecimal(0);
	    	log.info("names:"+names.length);
	    	log.info("ides:"+ids.length);
	    	Set<Long> effectIds=new HashSet<>();
	    	for (int i=0;i<ids.length;i++){
	    		log.info("names:"+names);
	    		if (!StringUtils.isBlank(ids[i])){
	    			Long id=Long.valueOf(ids[i]);
	    			effectIds.add(id);
	    		}
	    		sumHitRatio.add(new BigDecimal(Double.valueOf(hitRatios[i])));
	    	}
	    	if (sumHitRatio.doubleValue()>100){
		    	JsonResult jr=new JsonResult(JsonResult.ERROR,"所有奖品中奖率不能超过100%");
				return jr;
	    	}
	    	log.info("3333333333333333333");
	    	List<EventPrize> prizeList=eventPrizeService.getPrizes(event.getId());
	    	for (EventPrize eventPrize : prizeList) {
				if (effectIds.size()>0 ){
					if (!effectIds.contains(eventPrize.getId())){
						baseService.delete(eventPrize);
					}
				}else{
					baseService.delete(eventPrize);
				}
			}
	    	log.info("44444444444444444");
	    	for (int i=0;i<names.length;i++){
	    		if (!StringUtils.isBlank(ids[i])){
	    			Long id=Long.valueOf(ids[i]);
	    			ep =(EventPrize)baseService.loadById("EventPrize", id);
	    		}else{
	    			ep =new EventPrize();
	    		}
	    		ep.setName(names[i]);
	    		ep.setGrade(grades[i]);
	    		ep.setHitRatio(new BigDecimal(Double.valueOf(hitRatios[i])));
	    		ep.setMoney(new BigDecimal(Double.valueOf(moneys[i])));
	    		ep.setImage(images[i]);
	    		ep.setEvent(event);
	    		ep.setSortNob((long)(i+1));
	    		if (ep.getId()==null){
	    			baseService.create(ep);
	    		}else{
	    			baseService.update(ep);
	    		}
	    	}
	    	log.info("555555555555555555555555");
	    	
	    	JsonResult jr=new JsonResult(JsonResult.SUCCESS,"保存成功");
			return jr;
		}
	    /**
	     * 进入奖品配置页面
	     * @param request
	     * @param response
	     * @param session
	     * @param rhs
	     * @param eventUuid
	     * @return
	     */
	    @RequestMapping
	    public String eventPrize(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,String eventUuid){
			String uuid=getpara("eventUuid");
			Event event=(Event)EaCommonUtils.getObjectByUuid(uuid, "Event");
			List<EventPrize> prizeList=eventPrizeService.getPrizes(event.getId());
			if (prizeList.size()==0){
				EventPrize ep=new EventPrize();
				prizeList.add(ep);
			}
			rhs.addAttribute("prizeList", prizeList);
			rhs.addAttribute("event", event);
	    	return super.rtnDefault(request, response, session, rhs);
	    	
	    }
	    

		

		 
}
